<?php
/**
 * Created by PhpStorm.
 * User: yulin
 * Date:
 * Time:
 */

namespace app\adminapi\model;

use app\adminapi\filter;
use app\base\exception\RecordNotFoundException;
use think\Db;
use think\exception\DbException;

class CarScrap extends Common
{
    /**
     * 拍卖列表
     * @param string $fields
     * @param array $where
     * @param int $page
     * @param int $num
     * @return array
     */
    public function CarScrapList($fields='*',$where=[],$page=1,$num=10){
        $filter = new filter\CarScrapList();
        $filter->analyzeField();
        list(,$msg)      = $filter->checkField($fields);
        if($where['car_scrap_id']){
            $where2['car_scrap_id'] = $where['car_scrap_id'];
        }
        if($where['company_name']){
            $where2['company_name'] = $where['company_name'];
        }
        if($where['number']){
            $where2['number'] = $where['number'];
        }
        if($where['state']){
            $where2['state'] = $where['state'];
        }
        list(,$whereMsg) = $filter->checkWhere($where2);
        $whereMsg['a.delete_time'] = ["null"];
        $data['note'] = "查询条件json输入格式如{'car_scrap_id':['like','%王%'],'company_name':['like','%王%'],'number':['like','%王%'],'state':['in','1']}";

        $buildQuery = function($query) use ($whereMsg) {
            self::authCompanyQuery($query, $whereMsg, 'a.company_id');
        };

        $data['data'] = Db::name('car_scrap')
            ->alias('a')
            ->join('car c','a.car_id = c.car_id','left')
            ->join('car_brand d','c.car_brand_id = d.car_brand_id','left')
            ->join('car_brand e','c.car_series_id = e.car_brand_id','left')
            ->join('car_brand f','c.car_model_id = f.car_brand_id','left')
            ->join('car_use g','c.car_use_id = g.car_use_id','left')
            ->join('car_type h','c.car_type_id = h.car_type_id','left')
            ->join('company i','g.company_id = i.company_id','left')
            ->field($msg)
            ->where($buildQuery)
            ->order('date desc')
            ->page($page,$num)
            ->select();
        $data['count'] = Db::name('car_scrap')
            ->alias('a')
            ->join('car c','a.car_id = c.car_id','left')
            ->join('car_brand d','c.car_brand_id = d.car_brand_id','left')
            ->join('car_brand e','c.car_series_id = e.car_brand_id','left')
            ->join('car_brand f','c.car_model_id = f.car_brand_id','left')
            ->join('car_use g','c.car_use_id = g.car_use_id','left')
            ->join('car_type h','c.car_type_id = h.car_type_id','left')
            ->join('company i','g.company_id = i.company_id','left')
            ->where($buildQuery)
            ->order('date desc')
            ->count();
//        $data['data']        = $filter->checkData($data['data']);
        $data['description'] = $filter->title;
        $data['fields']      = $filter->fieldTitle;
        return [1,$data];
    }
    /**
     * 新增或者编辑购车申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function createOrUpdate($params) {
        $data = [];
        $data['note'] = "json输入格式如{}";

        $where = [];
        $params['car_scrap_id'] && $where['car_scrap_id'] = $params['car_scrap_id'];

        $update = [];
        $params['why'] && $update['why'] = $params['why'];
        $params['state'] && $update['state'] = $params['state'];
        $params['car_id'] && $update['car_id'] = $params['car_id'];
        $params['scrap_add'] && $update['scrap_add'] = $params['scrap_add'];
        $params['scrap_check'] && $update['scrap_check'] = $params['scrap_check'];
        $params['scrap_image'] && $update['scrap_image'] = $params['scrap_image'];

        if($params['car_scrap_id']) {

            if($update['state'] == '2'){
                $update['2_date'] = date("Y-m-d H:i:s");
            }
            if($update['state'] == '4'){
                $update['4_date'] = date("Y-m-d H:i:s");
                $car_id = Db::name('car_scrap')->where($where)->value('car_id');
                Db::name('car')->where('car_id',$car_id)->update(['delete_time' => time()]);
            }
            if($update['state'] == '6'){
                $update['6_date'] = date("Y-m-d H:i:s");
            }
            if($update['state'] == '7'){
                $update['7_date'] = date("Y-m-d H:i:s");
            }
            if($update['state'] == '8'){
                $update['8_date'] = date("Y-m-d H:i:s");
            }
            $data['data'] = Db::name('car_scrap')->where($where)->update($update);
            $data['count'] = $data['data'];
            return [1,$data];
        }
        else if($update['car_id']){

            $update['state'] = 1;
            $update['date'] = date("Y-m-d H:i:s");
            $data['data'] = Db::name('car_scrap')->insert($update);
            $where2 = [];
            $where2['car_id'] = $update['car_id'];
            Db::name('car')->where($where2)->update(['state' => '8']);
            $data['count'] = '1';
            return [1,$data];
        }
        else{
            $data = ['where' => $where, 'update' => $update , 'params' => $params];
            return [0,$data];
        }
    }

    /**
     * 删除购车申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function delete($id = '') {
        $data = [];
        $data['note'] = "json输入格式如{}";
        $data['data'] = Db::name('car_scrap')->where('car_scrap_id',$id)->update(['delete_time' => time()]);
        $car_id = Db::name('car_scrap')->where('car_scrap_id',$id)->value('car_id');
        Db::name('car')->where('car_id',$car_id)->update(['state' => '1']);
        $data['count'] = '1';
        return [1,$data];
    }
}